package com.lindenvalley.ch.content;

import java.io.Serializable;
import java.util.List;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import com.lindenvalley.ch.AbstractHomeActivity;

@Root(name="weeks")
public class Weeks implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Attribute(required=false)
	private int year;
	
	@ElementList(inline=true, name="week", required=false)
	private List<Week> weeks;

	public Weeks() {
		super();
	}

	/**
	 * This method return how many weeks have passed since start school year
	 * 
	 * @return the weekCount
	 */
	public int getWeekCount() {
		return weeks != null ? weeks.size() : 0;
	}

	/**
	 * @param year
	 *            the year to set
	 */
	public void setYear(int year) {
		this.year = year;
	}
	
	/**
	 * 
	 * @return
	 */
	public int getYear(){
		return this.year;
	}

	/**
	 * @return the taskInWeek
	 */
	public List<Week> getWeeks() {
		return weeks;
	}

	/**
	 * @param index
	 *            the index week of school year
	 */
	public Week getWeek(int index) {
		return weeks != null ? weeks.get(index) : null;
	}
	
	public Week getReverseWeek(int index) {
		return weeks != null ? weeks.get(weeks.size() - index - 1) : null;
	}

	/**
	 * @param taskInWeek
	 *            the taskInWeek to set
	 */
	public void addWeek(Week week) {
		weeks.add(week);
	}
	
	public int getWeekIndexByDate(long dateTime){
		int index = AbstractHomeActivity.NO_WEEK_INDEX;
		if (weeks != null){
			int count = 0;
			for(Week week: weeks){
				if (week.isContainsDate(dateTime)){
					index = count;
					break;
				}
				count++;
			}
		}
		
		return index;
		
	}

}
